<?php
class RecordAction extends AdministratorAction {

	// 地区管理
	public function area() {
		$areaPid = empty($_GET['areaPid'])?0:$_GET['areaPid'];
		$areaDao = D('Area');
		import("main.Org.Tree"); //导入分类树
		$fields = array('areaId','areaPid','areaName','areaSname');
		$tree = new Tree($areaDao,$fields);
		$areaList = $tree->getFirstChild($areaPid);
		$areaLangList = array();
		if(count($areaList)){
			foreach($areaList as $area){
				$areaIds[] = $area['areaId'];
			}
			//获取对应语言的
			$areaLangDao = D('AreaLang');
			unset($map);
			$map['_string'] = " (langName='".implode("' OR langName='",lang2b(explode(',',C('LANG_LIST'))))."' )";
			$map['_string'] .= " AND areaId IN (".implode(',',$areaIds).")";
			$areaLang = $areaLangDao->where($map)->findAll();
			foreach($areaLang as $OareaLang){
				$areaLangList[$OareaLang['areaId']][$OareaLang['langName']] = $OareaLang;
			}
		}
		$areaPath = $tree->getNicePathFromId($areaPid,'areaName',U('admin/Record/area')."&areaPid=","::");
		$this->assign('areaLangList', $areaLangList);
		$this->assign('areaPid', $areaPid);
		$this->assign('areaPath', $areaPath);
		$this->assign('areaList', $areaList);
        $this->display();
	}
	public function editArea() {
		$areaDao = D('Area');
		$areaPid = empty($_GET['areaPid'])?0:$_GET['areaPid'];
		$areaId = empty($_GET['areaId'])?0:$_GET['areaId'];
		$areaLangInfo = array();
		if($areaId){
			$areaInfo = $areaDao->find($areaId);
			$areaLangDao = D('AreaLang');
			$map['_string'] = " (langName='".implode("' OR langName='",lang2b(explode(',',C('LANG_LIST'))))."' )";
			$map['_string'] .= " AND areaId = '".$areaId."'";
			$areaLang = $areaLangDao->where($map)->findAll();
			foreach($areaLang as $OareaLang){
				$areaLangInfo[$OareaLang['langName']] = $OareaLang;
			}
		}else{
			foreach(lang2b(explode(',',C('LANG_LIST'))) as $l){
				if($l!='en_US') $areaLangInfo[$l] = '';
			}
			$areaInfo = $areaDao->create();
		}
		$this->assign('areaLangInfo', $areaLangInfo);
		$this->assign('areaInfo', $areaInfo);
		$this->assign('areaId', $areaId);
		$this->assign('areaPid', $areaPid);
        $this->display();
	}

	public function doSaveArea() {
		$areaDao = D('Area');
		$areaLangDao = D('AreaLang');
		if(empty($_POST['areaId'])){
			$areaDao->create();
			$nId=$areaDao->add($_POST);
			$areaLangDao->create();
			foreach($_POST['lang'] as $lang=>$langInfo){
				$langInfo['langName'] = $lang;
				$langInfo['areaId'] = $nId;
				$lId=$areaLangDao->add($langInfo);
			}
		}else{
			$nId=$areaDao->save($_POST);
			foreach($_POST['lang'] as $lang=>$langInfo){
				$areaLang = $areaLangDao->where("langName='".$lang."' AND areaId='".$_POST['areaId']."'")->data($langInfo)->save();
			}
		}
		redirect(U('admin/Record/area',array('areaPid'=>$_POST['areaPid'])));
	}

	// 类别管理
	public function category() {
		$catPid = (empty($_GET['catPid']) || !is_numeric(trim($_GET['catPid']))) ? 0 : trim($_GET['catPid']);
		$map['catPid'] = $catPid;
		$catD = D('Category','main');

		import("main.Org.Tree"); //导入分类树
		$fields = array('catId','catPid','catTitle','catCode');
		$tree = new Tree($catD,$fields);
		$catList = $tree->getFirstChild($catPid);
		$catLangList = array();
		if(count($catList)){
			foreach($catList as $cat){
				$catIds[] = $cat['catId'];
			}
			//获取对应语言的
			$catLangDao = D('CategoryLang','main');
			unset($map);
			$map['_string'] = " (langName='".implode("' OR langName='",lang2b(explode(',',C('LANG_LIST'))))."' )";
			$map['_string'] .= " AND catId IN (".implode(',',$catIds).")";
			$catLang = $catLangDao->where($map)->findAll();
			foreach($catLang as $OcatLang){
				$catLangList[$OcatLang['catId']][$OcatLang['langName']] = $OcatLang;
			}
		}
		$catPath = $tree->getNicePathFromId($catPid,'catTitle',U('admin/Record/category')."&catPid=","::");
		$this->assign('catLangList', $catLangList);
		$this->assign('catPid', $catPid);
		$this->assign('catPath', $catPath);
		$this->assign('catList', $catList);
        $this->display();



	}
	public function editCategory() {
		$catDao = D('Category','main');
		$catPid = empty($_GET['catPid'])?0:$_GET['catPid'];
		$catId = empty($_GET['catId'])?0:$_GET['catId'];
		$catLangInfo = array();
		if($catId){
			$catInfo = $catDao->find($catId);
			$catLangDao = D('CategoryLang','main');
			$map['_string'] = " (langName='".implode("' OR langName='",lang2b(explode(',',C('LANG_LIST'))))."' )";
			$map['_string'] .= " AND catId = '".$catId."'";
			$catLang = $catLangDao->where($map)->findAll();
			foreach($catLang as $OcatLang){
				$catLangInfo[$OcatLang['langName']] = $OcatLang;
			}
		}else{
			foreach(lang2b(explode(',',C('LANG_LIST'))) as $l){
				if($l!='en_US') $catLangInfo[$l] = '';
			}
			$catInfo = $catDao->create();
		}
		$this->assign('catLangInfo', $catLangInfo);
		$this->assign('catInfo', $catInfo);
		$this->assign('catId', $catId);
		$this->assign('catPid', $catPid);
        $this->display();
	}

	public function doSaveCategory() {
		$catDao = D('Category','main');
		$catLangDao = D('CategoryLang','main');
		if(empty($_POST['catId'])){
			$catDao->create();
			$nId=$catDao->add($_POST);
			$catLangDao->create();
			foreach($_POST['lang'] as $lang=>$langInfo){
				$langInfo['langName'] = $lang;
				$langInfo['catId'] = $nId;
				$lId=$catLangDao->add($langInfo);
			}
		}else{
			$nId=$catDao->save($_POST);
			foreach($_POST['lang'] as $lang=>$langInfo){
				$catLang = $catLangDao->where("langName='".$lang."' AND catId='".$_POST['catId']."'")->data($langInfo)->save();
			}
		}
		redirect(U('admin/Record/category',array('catPid'=>$_POST['catPid'])));
	}

	// 公司认证及资质
	public function certifications() {
        $this->display();
	}

	public function port() {
		$portContinent = D('Area')->getContinent();
		$this->assign('portContinent', $portContinent);
        $this->display();
	}

}